﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using VkontakteCore;
using VkontakteInfrastructure.Model;

namespace VkVideoPlayer.UI.ViewModels
{
	public class AuthViewModel : ViewModelBase
	{
		private const string AppId = "2758719";
		public AuthViewModel()
		{

		}

		public override void OnNavigatedFrom(Microsoft.Phone.Controls.PhoneApplicationPage page,
		                                     System.Windows.Navigation.NavigationEventArgs e)
		{
			base.OnNavigatedFrom(page, e);
		}

		public override void OnNavigatedTo(Microsoft.Phone.Controls.PhoneApplicationPage page,
		                                   System.Windows.Navigation.NavigationEventArgs e)
		{
			base.OnNavigatedTo(page, e);
		}

		public bool IsAuthorized
		{
			get { return GetService<IVkontakteApi>().Authorized(); }
		}

		public void RestoreContext()
		{
			GetService<IVkontakteApi>().RestoreContext();
		}

		public Uri GetUri()
		{
			var uri = new Uri(AuthorizationHelper.GetAuthorizationUrl(AppId));
			return uri;
		}
		public void ParseUri(Uri uri)
		{
			var result = AuthorizationHelper.ParseNavigatedUrl(uri.ToString());
			if (result.Status == AuthorzationStatus.Success)
			{
				this.GetService<IVkontakteApi>().Initialize(result.Context);
				(Application.Current.RootVisual as PhoneApplicationFrame).GoBack();
			}
		}
	}
}
